<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>The Yaf_Router class</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="yaf-route-rewrite.route.html">« Yaf_Route_Rewrite::route</a></li>
      <li style="float: right;"><a href="yaf-router.addconfig.html">Yaf_Router::addConfig »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="book.yaf.html">Yaf</a></li>
    <li>The Yaf_Router class</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="class.yaf-router" class="reference">

 <h1 class="title">The Yaf_Router class</h1>
 

 <div class="partintro"><p class="verinfo">(Yaf &gt;=1.0.0)</p>


  <div class="section" id="yaf-router.intro">
   <h2 class="title">简介</h2>
   <p class="para">
   <span class="classname"><strong class="classname">Yaf_Router</strong></span>是标准的框架路由.路由是一个拿到URI端点（URL中的URI的一部分）然后分解参数得到哪一个module, controller, 和action需要接受请求。module, controller, 和action，还有一些其他的参数是打包在一个<span class="classname"><a href="class.yaf-request-abstract.html" class="classname">Yaf_Request_Abstract</a></span>的对象中，然后通过<span class="classname"><a href="class.yaf-dispatcher.html" class="classname">Yaf_Dispatcher</a></span>来处理的。路由只发生一次:最初接到请求并且在第一个controller分发之前。
   <span class="classname"><strong class="classname">Yaf_Router</strong></span> 是设计来允许使用纯PHP结构的类似功能模块的跳转。它非常松散的基于Ruby on Rails的路由，并且不需要你提前就知道webserver URL跳转的相关知识。它是设计来处理一个Apache 跳转模块的规则（一个）
   <span class="classname"><strong class="classname">Yaf_Router</strong></span>是设计来允许mod_rewrite
   <div class="example" id="example-3930">
    <p><strong>示例 #1 Apache的跳转规则</strong></p>
   <div class="example-contents">
<div class="confcode"><pre class="confcode">RewriteEngine on
RewriteRule !\.(js|ico|gif|jpg|png|css|html)$ index.php</pre>
</div>
   </div>

   </div>
   or (preferred):
   <div class="example" id="example-3931">
   <p><strong>示例 #2 Apache的跳转规则</strong></p>
   <div class="example-contents">
<div class="confcode"><pre class="confcode">RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]</pre>
</div>
   </div>

  </div>
   如果使用Lighttpd，下面的跳转规则是有效的：
   <div class="example" id="example-3932">
    <p><strong>示例 #3 Lighttpd的跳转规则</strong></p>
   <div class="example-contents">
<div class="confcode"><pre class="confcode">url.rewrite-once = (
  &quot;.*\?(.*)$&quot; =&gt; &quot;/index.php?$1&quot;,
  &quot;.*\.(js|ico|gif|jpg|png|css|html)$&quot; =&gt; &quot;$0&quot;,
  &quot;&quot; =&gt; &quot;/index.php&quot;
)</pre>
</div>
   </div>

  </div>
   如果使用Nginx，下面的跳转规则是有效的：
   <div class="example" id="example-3933">
    <p><strong>示例 #4 Nginx的跳转规则</strong></p>
   <div class="example-contents">
<div class="confcode"><pre class="confcode">server {
  listen ****;
  server_name  yourdomain.com;
  root   document_root;
  index  index.php index.html;

  if (!-e $request_filename) {
    rewrite ^/(.*)  /index.php/$1 last;
  }
}</pre>
</div>
   </div>

  </div>
   </p>
  </div>


  <div class="section" id="yaf-router.default">
   <h2 class="title">默认路由</h2>
   <p class="para">
   Yaf_Router预设了一个默认路由，它将以controller/action的形式匹配URIs。此外，一个module的名字可以被指定为第一路径元素，允许URIs设置为module/controller/action的形式。最后，它也会匹配一些URI中额外附加的参数，默认形式是controller/action/var1/value1/var2/value2。
    <blockquote class="note"><p><strong class="note">注意</strong>: 
     <p class="para">
      Module的名字必须要定义在配置中，就application.module=&quot;Index,Foo,Bar&quot;而言，在这种情况下，仅仅index, foo 和 bar能被考虑作为为一个module的名称。如果没有在配置文件中定义，那么Yaf使用默认的module名字“Index”。
     </p>
    </p></blockquote>
   </p>

   <p class="para">
   如何匹配这些路由的一些例子：
   <div class="example" id="example-3934">
    <p><strong>示例 #5 <span class="classname"><a href="class.yaf-route-static.html" class="classname">Yaf_Route_Static</a></span>(default
     route)example</strong></p>
   <div class="example-contents">
<div class="confcode"><pre class="confcode">// Assuming the following configure:
$conf = array(
   &quot;application&quot; =&gt; array(
      &quot;modules&quot; =&gt; &quot;Index,Blog&quot;,
   ),
);

Controller only:
http://example/news
    controller == news
Action only(when defined yaf.action_prefer=1 in php.ini)
    action  == news
 
Invalid module maps to controller name:
http://example/foo
    controller == foo
 
Module + controller:
http://example/blog/archive
    module     == blog
    controller == archive
 
Module + controller + action:
http://example/blog/archive/list
    module     == blog
    controller == archive
    action     == list
 
Module + controller + action + params:
http://example/blog/archive/list/sort/alpha/date/desc
    module     == blog
    controller == archive
    action     == list
    sort       == alpha
    date       == desc</pre>
</div>
   </div>

  </div>

  </p>
  </div>

  <div class="section" id="yaf-router.synopsis">
   <h2 class="title">类摘要</h2>


   <div class="classsynopsis">
    <div class="ooclass"></div>


    <div class="classsynopsisinfo">
     <span class="ooclass">
      <span class="modifier">class</span> <strong class="classname">Yaf_Router</strong>
     </span>
     {</div>

    <div class="classsynopsisinfo classsynopsisinfo_comment">/* 属性 */</div>
    <div class="fieldsynopsis">
     <span class="modifier">protected</span>
      <var class="varname"><a href="class.yaf-router.html#yaf-router.props.routes">$<var class="varname">_routes</var></a></var>;</div>

    <div class="fieldsynopsis"><span class="modifier">protected</span>
      <var class="varname"><a href="class.yaf-router.html#yaf-router.props.current">$<var class="varname">_current</var></a></var>;</div>


    
    <div class="classsynopsisinfo classsynopsisinfo_comment">/* 方法 */</div>
    <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><a href="yaf-router.addconfig.html" class="methodname">addConfig</a></span>(<span class="methodparam"><span class="type"><a href="class.yaf-config-abstract.html" class="type Yaf_Config_Abstract">Yaf_Config_Abstract</a></span> <code class="parameter">$config</code></span>): <span class="type"><span class="type void">void</span></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="yaf-router.addroute.html" class="methodname">addRoute</a></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$name</code></span>, <span class="methodparam"><span class="type"><span class="type Yaf_Route_Abstract">Yaf_Route_Abstract</span></span> <code class="parameter">$route</code></span>): <span class="type"><a href="class.yaf-router.html" class="type Yaf_Router">Yaf_Router</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span><span class="methodname"><a href="yaf-router.construct.html" class="methodname">__construct</a></span>()</div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="yaf-router.getcurrentroute.html" class="methodname">getCurrentRoute</a></span>(): <span class="type">string</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="yaf-router.getroute.html" class="methodname">getRoute</a></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$name</code></span>): <span class="type"><span class="type void">void</span></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="yaf-router.getroutes.html" class="methodname">getRoutes</a></span>(): <span class="type"><span class="type void">void</span></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="yaf-router.route.html" class="methodname">route</a></span>(<span class="methodparam"><span class="type"><a href="class.yaf-request-abstract.html" class="type Yaf_Request_Abstract">Yaf_Request_Abstract</a></span> <code class="parameter">$request</code></span>): <span class="type">bool</span></div>

   }</div>


  </div>

  

  <div class="section" id="yaf-router.props">
   <h2 class="title">属性</h2>
   <dl>
    
     <dt id="yaf-router.props.routes"><var class="varname">_routes</var></dt>

     <dd>

      <p class="para"/>
     </dd>

    
    
     <dt id="yaf-router.props.current"><var class="varname">_current</var></dt>

     <dd>

      <p class="para"/>
     </dd>

    
   </dl>

  </div>



 </div>

 


















































<h2>目录</h2><ul class="chunklist chunklist_reference"><li><a href="yaf-router.addconfig.html">Yaf_Router::addConfig</a> — 向Router中添加配置文件中定义的路由</li><li><a href="yaf-router.addroute.html">Yaf_Router::addRoute</a> — 往Router中添加新的路由</li><li><a href="yaf-router.construct.html">Yaf_Router::__construct</a> — Yaf_Router constructor</li><li><a href="yaf-router.getcurrentroute.html">Yaf_Router::getCurrentRoute</a> — 取得当前有效的路由名</li><li><a href="yaf-router.getroute.html">Yaf_Router::getRoute</a> — The getRoute purpose</li><li><a href="yaf-router.getroutes.html">Yaf_Router::getRoutes</a> — The getRoutes purpose</li><li><a href="yaf-router.route.html">Yaf_Router::route</a> — The route purpose</li></ul>
</div>
</div></div></body></html>